<?php
namespace app\stores\logic;
use app\beautiful\model\logs\MemberBalanceLogsModel;
use app\beautiful\model\member\MemberModel;
use app\beautiful\model\LevelModel;
use app\beautiful\model\setting\LevelSettingModel;
use app\stores\model\member\MemberLevelModel;

//用户等级
class MemberLevelLogic{
   
   //等级是充值金额
   public static  function level($shopId,$memberId,$balance = 0 ,$integral = 0){
       $member = MemberModel::find($memberId);
       if(null === $member) return false;
       $max_level_id = 0;
       //单次充值
       if(null !== $Level = LevelModel::where([
               ['shop_id','=',$shopId],
               ['type','=',1],
               ['is_delete','=',0]
           ])->where(['need_recharge'=>$balance])->order(['vip_level'=>'desc'])->find()){

           $max_vip_level = $Level->vip_level;
           $max_level_id = $Level->level_id;
       }
       //累计充值
       $total_balance = MemberBalanceLogsModel::where([
           ['shop_id','=',$shopId],
           ['type_id','=',3],
           ['type','=',1],
           ['member_id','=',$memberId]
       ])->sum('balance');

       if(null !== $Level = LevelModel::where([
           ['shop_id','=',$shopId],
           ['type','=',1],
           ['is_delete','=',0]
       ])->where("need_recharge <= ".$total_balance)->order(['vip_level'=>'desc'])->find()){
           if($Level->vip_level > $max_vip_level) $max_level_id = $Level->level_id;
       }
       //是否已经是当前等级
       if($max_level_id>0){
           //有效期
           $level_set = LevelSettingModel::where(['shop_id'=>$shopId,'is_selected'=>1,'type'=>1])->find();
           if($level_set){
               if($Level->time_type == 1){
                   $vip_start_time = date("Y-m-d")." 00:00:00";
                   $vip_end_time = date("Y-m-d",strtotime("+1 year"))." 23:59:59";
               }
               if($Level->time_type == 3){
                   $vip_start_time = date("Y-m-d")." 00:00:00";
                   $vip_end_time = date("Y-m-d",strtotime("+1 month"))." 23:59:59";
               }
               if($Level->time_type == 4){
                   $vip_start_time = date("Y-m-d")." 00:00:00";
                   $vip_end_time = date("Y-m-d",strtotime("+3 month"))." 23:59:59";
               }
               $member_level = MemberLevelModel::where(['shop_id'=>$shopId,'member_id'=>$memberId,'is_delete'=>0])->find();
               if(empty($member_level)){
                   $member_level = new MemberLevelModel();
                   $member_level->shop_id = $shopId;
                   $member_level->member_id = $memberId;
               }
               $member_level->level_id = $max_level_id;
               $member_level->vip_start_time = $vip_start_time;
               $member_level->vip_end_time = $vip_end_time;
               $member_level->vip_time_type = $level_set->time_type;
               $member_level->edit_time = date('Y-m-d H:i:s');
               $member_level->save();
           }


       }

       return  true;
   }    
}